{% extends "learning/base.html" %}
{% load learning_extras %}

{% block title %}专题训练 - 高中数学助手{% endblock %}

{% block content %}
<div class="card">
    <div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:2rem;flex-wrap:wrap;gap:1rem;">
        <div>
            <h2 style="margin:0 0 0.5rem 0;">📚 专题训练</h2>
            <p style="margin:0;color:var(--text-secondary);font-size:0.9375rem;">
                选择专题，通过智能跳题逻辑进行针对性训练，AI助教实时解析
            </p>
        </div>
    </div>

    {% if trainings %}
    <div class="dashboard-grid">
        {% for training in trainings %}
        <div class="stat-card" style="cursor:pointer;" onclick="window.location.href='{% url 'learning:topic_training_start' training.pk %}';">
            <div style="display:flex;align-items:start;justify-content:space-between;margin-bottom:1rem;">
                <h3 style="margin:0;font-size:1.125rem;font-weight:600;">{{ training.title }}</h3>
                {% if training.pk in progress_dict %}
                    {% with progress=progress_dict|get_item:training.pk %}
                    {% if progress.is_completed %}
                        <span class="pill success">✓ 已完成</span>
                    {% else %}
                        <span class="pill warning">⏸ 进行中</span>
                    {% endif %}
                    {% endwith %}
                {% else %}
                    <span class="pill">🆕 未开始</span>
                {% endif %}
            </div>
            
            {% if training.description %}
            <p style="margin:0 0 1rem 0;color:var(--text-secondary);font-size:0.875rem;line-height:1.6;">
                {{ training.description|truncatewords:20 }}
            </p>
            {% endif %}
            
            <div style="display:flex;align-items:center;justify-content:space-between;padding-top:1rem;border-top:1px solid var(--border-subtle);">
                <span style="font-size:0.8125rem;color:var(--text-tertiary);">
                    📝 共 {{ training.questions.count }} 道题
                </span>
                
                {% if training.pk in progress_dict %}
                    {% with progress=progress_dict|get_item:training.pk %}
                    {% if progress.is_completed %}
                        <a href="{% url 'learning:topic_training_start' training.pk %}" class="button small">
                            重新开始
                        </a>
                    {% else %}
                        <a href="{% url 'learning:topic_training_question' training.pk %}" class="button small">
                            继续训练
                        </a>
                    {% endif %}
                    {% endwith %}
                {% else %}
                    <a href="{% url 'learning:topic_training_start' training.pk %}" class="button small">
                        开始训练
                    </a>
                {% endif %}
            </div>
        </div>
        {% endfor %}
    </div>
    {% else %}
    <div style="text-align:center;padding:4rem 2rem;">
        <div style="font-size:4rem;margin-bottom:1.5rem;opacity:0.3;">📚</div>
        <p style="margin:0;color:var(--text-secondary);font-size:1.125rem;">
            暂无可用的专题训练
        </p>
        <p style="margin:0.5rem 0 0 0;color:var(--text-tertiary);font-size:0.9375rem;">
            请联系老师添加训练内容
        </p>
    </div>
    {% endif %}
</div>

{% if progress_dict %}
<div class="card">
    <h3 style="margin:0 0 1.5rem 0;">📊 我的训练记录</h3>
    
    <div class="feed">
        {% for training in trainings %}
            {% if training.pk in progress_dict %}
            {% with progress=progress_dict|get_item:training.pk %}
            <div class="feed-item">
                <div class="feed-header">
                    <div>
                        <h4 class="feed-title">{{ training.title }}</h4>
                        <div class="feed-meta">
                            <span>📅 开始于 {{ progress.started_at|date:"Y-m-d H:i" }}</span>
                            {% if progress.is_completed %}
                                <span>✓ 完成于 {{ progress.completed_at|date:"Y-m-d H:i" }}</span>
                            {% endif %}
                            <span>📝 已答 {{ progress.answer_history|length }} 题</span>
                        </div>
                    </div>
                    <div>
                        {% if progress.is_completed %}
                            <span class="pill success">已完成</span>
                        {% else %}
                            <span class="pill warning">进行中</span>
                        {% endif %}
                    </div>
                </div>
                
                <div class="action-group">
                    {% if progress.is_completed %}
                        <a href="{% url 'learning:topic_training_start' training.pk %}" class="button small secondary">
                            🔄 重新开始
                        </a>
                    {% else %}
                        <a href="{% url 'learning:topic_training_question' training.pk %}" class="button small">
                            ▶️ 继续训练
                        </a>
                    {% endif %}
                </div>
            </div>
            {% endwith %}
            {% endif %}
        {% endfor %}
    </div>
</div>
{% endif %}

{% endblock %}
